import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../../shared/smartadmin.config';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-abnormalMonitor',
	templateUrl: './abnormalMonitor.component.html',
	styleUrls: ['./abnormalMonitor.component.css']
})
export class AbnormalMonitorComponent implements OnInit {
	tableData: any[] = []; //table数据
	totalCount: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	searchKey: string = ''; //搜索keyword
	carList: any[] = [];
	companyId: string = '';
	deviceId: string = '';//设备ID
	startTime_table: string = '';
	startTime_chart: string = '';
	endTime_table: string = '';
	endTime_chart: string = '';
	abnormalType = '1';
	minExceptionTime = '';//最小异常时间
	carNumber: string = ''; //车牌号码
	selectedCompanyId: string = '';
	selectedCompanyName: string = '';
	activeSelected: string = '';
	selectedRow: any;
	isChartView = false;

	//画轨迹的数据
	drawTrackArr: any;
	isMapView = false;
	specifyId = {
		picker1: 'datetimepicker3',
		picker2: 'datetimepicker4'
	}
	specifyTime = {
		startTime: '',
		endTime: '',
	}
	constructor(private jsonApiService: JsonApiService, private router: Router) { }
	// 接收子组件传递过来的数据 
	getSelectTableTime(time) {
		this.startTime_table = time.startTime;
		this.endTime_table = time.endTime;
	}
	getSelectChartTime(time) {
		this.startTime_chart = time.startTime;
		this.endTime_chart = time.endTime;
	}
	runParent(value) {
		this.drawTrackArr = value;
	}
	showMapView(val) {
		this.isMapView = val;
	}
	checkAbnormal(row) {
		this.activeSelected = row.id;
		this.startTime_chart = common.getTimeMmss(new Date(common.mmsstoTimestamp(row.startDate) - 5 * 60 * 60 * 1000));
		this.endTime_chart = common.getTimeMmss(new Date(common.mmsstoTimestamp(row.endDate) + 5 * 60 * 60 * 1000));
		this.isChartView = true;
		row.startTime = this.startTime_chart;
		row.endTime = this.endTime_chart;
		this.specifyTime = {
			startTime: this.startTime_chart,
			endTime: this.endTime_chart,
		}
		this.selectedRow = row;
	}
	ngOnInit() {
		this.load()
	}
	drawAbnormalChart() {
		if (!this.selectedRow) {
			this.jsonApiService.message('先选择需要查看折线图的车辆');
			return;
		}
		if (!this.startTime_chart || !this.endTime_chart) {
			this.jsonApiService.message('异常起始时间不能为空');
			return;
		}
		let timeCheck = common.correctTimeParams(this.startTime_chart, this.endTime_chart, 0);
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let params = {
			deviceId: this.selectedRow.deviceId,
			startTime: this.startTime_chart,
			endTime: this.endTime_chart,
		}
		this.selectedRow = params;


	}
	//跳转到数据优化页面
	jumpOptimization(row) {
		var junmpData = {
			deviceId: row.deviceId,
			carNumber: row.carNumber,
			startDate: row.startDate,
			endDate: row.endDate
		}
		this.router.navigate(['/system-config/data-optimization'], { queryParams: junmpData });
		setTimeout(function () {
			$('#left-panel').find('.top-menu-invisible.open').find('.UL').css('display', 'none');
			$('#left-panel').find('.top-menu-invisible.open').removeClass('open');
			$('#left-panel').find('.top-menu-invisible.active').addClass('open');
			$('#left-panel').find('.top-menu-invisible.active').find('.UL').css('display', 'block');
		}, 300)
	}
	delete_row(row) {
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("dataExceptionMonitor/deleteDataInfoById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				this.load();
			})
		})
	}
	//  初始化加载list数据
	load() {
		let timeCheck = common.correctTimeParams(this.startTime_table, this.endTime_table, 0);
		if (!timeCheck.status) {
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let toNumberTime: any = Math.floor(+this.minExceptionTime * 3600);
		if (this.minExceptionTime && toNumberTime < 0) {
			this.jsonApiService.message('最小异常时间不能小于0');
			return;
		}
		if (toNumberTime <= 0) {
			toNumberTime = '';
		} else {
			toNumberTime = toNumberTime.toString();
		}

		let data = {
			pageSize: this.pageSize,
			nowPage: this.curPage,
			parameters: {
				companyId: this.selectedCompanyId,
				deviceId: this.deviceId,
				carNumber: this.carNumber,
				startTime: this.startTime_table,
				endTime: this.endTime_table,
				exceptionType: this.abnormalType,
				exceptionTime: toNumberTime,
				searchKey: this.searchKey
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(data);
		this.jsonApiService.http("dataExceptionMonitor/dataExceptionMonitorPage.html", reqdata).subscribe(result => {
			if (!result.isSuccess) {
				this.jsonApiService.message(result.message);
				return;
			}
			for (let item of result.exhibitDatas) {
				item.exceptionTime = common.secondToDate(item.exceptionTime);
			}
			this.tableData = result.exhibitDatas;
			this.totalCount = result.recordCount;
		})
	}
	ngOnDestroy() {
		document.onclick = null;
	}
	//刷新
	refresh() {
		this.load();
	}
	//查询
	table_search() {
		this.curPage = 1;
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	closeChart() {
		this.isChartView = false;
	}
	//公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
}
